package com.entrogy.service.impl;

import com.Sinewecology.EntropyWechat.MessageUtil;
import com.Sinewecology.EntropyWechat.NewsMessage;
import com.Sinewecology.EntropyWechat.WX_UserUtil;
import com.entrogy.AccessToken.AccessTokenUpdate;
import com.entrogy.bean.PubCountEntity;
import com.entrogy.dao.PubcountMapper;
import com.entrogy.utils.RedisUtil;
import com.entrogy.utils.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author xlr
 * @description 微信处理的核心实现类
 */
@Component
public class WeixinCoreServiceImpl {

	private static WeixinCoreServiceImpl coreService;
	private static RedisUtil redisUtil = new RedisUtil();

	@Autowired
	private PubcountMapper pubcountMapper;

	@PostConstruct
	public void init(){
		coreService = this;
		coreService.pubcountMapper = this.pubcountMapper;
	}
	/*
	 * 处理微信发来的请求
	 * @param request
	 * @return xml
	 */
	public static String processRequest(HttpServletRequest request) {
		// xml格式的消息数据
		String respXml = null;
		// 默认返回的文本消息内容
		String respContent = "未知的消息类型！";
		try {
			// ????parseXml???????????????
			@SuppressWarnings("rawtypes")
			Map requestMap = MessageUtil.parseXml(request);
			// ????????
			String fromUserName = requestMap.get("FromUserName").toString();
			// System.out.println("FromUserName:"+fromUserName);
			// ??????????
			String toUserName = requestMap.get("ToUserName").toString();
			// System.out.println("ToUserName:"+toUserName);
			// ???????
			String msgType = requestMap.get("MsgType").toString();

			if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
				// ???????
				String eventType = requestMap.get("Event").toString();
				// ???
				if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
					respContent = "谢谢您的关注！";
					PubCountEntity pcb = WX_UserUtil.getPubCountUsrInformation((String) redisUtil.get(toUserName), fromUserName);
					if (null != pcb && !StrUtils.isNullOrEmpty(pcb.getOpenid())){
						List<PubCountEntity> pubCountEntities = coreService.pubcountMapper.getPubCountEntityList(pcb.getUnionid());
						if(pubCountEntities.size() > 0)
							 coreService.pubcountMapper.updatePubcountInformation(pcb.getUnionid(), pcb.getOpenid(),toUserName);
						else
							 coreService.pubcountMapper.insertPubcountInformation(pcb.getUnionid(), pcb.getOpenid(),toUserName);
					}
					NewsMessage news = new NewsMessage();
					/*List<Article> articles = new ArrayList<Article>();
					Article article = new Article();
					article.setTitle("点击阅读智慧校园使用说明！");
					article.setDescription("智慧校园");
					article.setPicUrl(
							"https://mmbiz.qpic.cn/mmbiz_jpg/Xo1cGI0SMTqya8pJfdxhqUOwiaVibuiaJvHECJWibp8QacRr3TJqe46dQnaPibT53f7SOKyVfAFS7GyyHflXcoCSic8w/0?wx_fmt=jpeg");
					article.setUrl("https://mp.weixin.qq.com/s/qnBIt9KHoph6yMRYjjtTAA");
					articles.add(article);

					news.setArticles(articles);*/
					news.setCreateTime(System.currentTimeMillis());
					news.setToUserName(fromUserName);
					news.setFromUserName(toUserName);
					respXml = news.toString();
					// System.out.println("respXml: "+respXml);
				}
				// ??????
				else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {

				}
				// ?????????????
				else if (eventType.equals(MessageUtil.EVENT_TYPE_SCAN)) {
				}
				// ???????λ??
				else if (eventType.equals(MessageUtil.EVENT_TYPE_LOCATION)) {
				}
				// ???????
				else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {
				}
			}
			// ????????????????
//	            textMessage.setContent(respContent);
			// ?????????????????xml
//	            respXml = MessageUtil.messageToXml(textMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return respXml;
	}

}
